from Object3D import *
from OpenGL.GL import *
from OpenGL.GLU import *

class Racket(Object3D):

	def __init__(self, parent=None):
		super(Racket, self).__init__()
		self.parent = parent
		self.radius = 0.06
		self.rotation = 0.0
		self.rotationX = 10.0
		self.alpha = 1.0
		self.attacking = False
		self.rotXIncrement = 0.0
		self.positionIncrement = 0.0

	def draw(self):

		glPushMatrix()
		glTranslatef(*self.position.list())
		glRotatef(self.rotation, 0,-1.0,0)
		glRotatef(self.rotationX, -1.0,0,0)

		glPushMatrix()

		glMaterialfv(GL_FRONT, GL_AMBIENT, [0.1,0.1,0.1,self.alpha])
		glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.0,0.0,0.0,self.alpha])
		glMaterialfv(GL_FRONT, GL_SPECULAR, [0.2,0.2,0.2,self.alpha])

		glTranslatef(0,0,self.radius/20.0)
		disk1 = gluNewQuadric()
		gluQuadricOrientation(disk1, GLU_OUTSIDE)
		gluDisk(disk1, 0, self.radius, 20, 1)

		glMaterialfv(GL_FRONT, GL_AMBIENT, [0.6,0.1,0.1,self.alpha])
		glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.8,0.1,0.1,self.alpha])
		glMaterialfv(GL_FRONT, GL_SPECULAR, [0.2,0.2,0.2,self.alpha])

		glTranslatef(0,0,-self.radius/10.0)
		glRotatef(180.0,1.0,0,0)
		disk2 = gluNewQuadric()
		gluQuadricOrientation(disk2, GLU_OUTSIDE)
		gluDisk(disk2, 0, self.radius, 20, 1)

		glPopMatrix()

		glMaterialfv(GL_FRONT, GL_AMBIENT, [0.6,0.4,0.2,1.0])
		glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.9,0.7,0.4,1.0])
		glMaterialfv(GL_FRONT, GL_SPECULAR, [0.2,0.2,0.2,1.0])

		glTranslatef(0,0,-self.radius/10.0)
		cylinder1 = gluNewQuadric()
		gluQuadricOrientation(cylinder1, GLU_OUTSIDE)
		gluCylinder(cylinder1, self.radius, self.radius, self.radius/10.0, 20, 1)
		glTranslatef(0,0,self.radius/10.0)

		glTranslatef(-self.radius/1.5, 0, 0)
		glRotatef(90,0,-1.0,0)
		cylinder2 = gluNewQuadric()
		gluQuadricOrientation(cylinder2, GLU_OUTSIDE)
		gluCylinder(cylinder2, self.radius/5.0, self.radius/5.0, self.radius, 20, 1)

		glRotatef(180,0,1.0,0)
		disk3 = gluNewQuadric()
		gluQuadricOrientation(disk3, GLU_OUTSIDE)
		gluDisk(disk3, 0, self.radius/5.0, 20, 1)
		glRotatef(180,0,1.0,0)

		glTranslatef(0,0,self.radius)
		disk4 = gluNewQuadric()
		gluQuadricOrientation(disk4, GLU_OUTSIDE)
		gluDisk(disk4, 0, self.radius/5.0, 20, 1)

		glPopMatrix()
		
		# shadow
		glPushMatrix()
		glMaterialfv(GL_FRONT, GL_AMBIENT, [0.15,0.15,0.15,0.7])
		glMaterialfv(GL_FRONT, GL_DIFFUSE, [0.15,0.15,0.15,0.7])
		glMaterialfv(GL_FRONT, GL_SPECULAR, [0.0,0.0,0.0,1.0])
		glTranslatef(self.position.x, 0.02, self.position.z)
		glRotatef(self.rotation, 0,-1.0,0)
		size = 1.0 + self.position.y
		if ((self.position.z > self.parent.table.depth/2.0) or
		(self.position.z < -self.parent.table.depth/2.0) or
		(self.position.x > self.parent.table.width/2.0) or
		(self.position.x < -self.parent.table.width/2.0)):
			glTranslatef(0,-self.parent.table.height,0)
			size += self.parent.table.height
		glBegin(GL_QUADS)
		glNormal3f(0.0,1.0,0.0)
		glVertex3f(-self.radius*size, 0.0, -self.radius*size/20.0)
		glVertex3f(-self.radius*size, 0.0, self.radius*size/20.0)
		glVertex3f(self.radius*size, 0.0, self.radius*size/20.0)
		glVertex3f(self.radius*size, 0.0, -self.radius*size/20.0)
		
		glVertex3f(-self.radius*size/2.0 -self.radius*size, 0.0, -self.radius*size/5.0)
		glVertex3f(-self.radius*size/2.0 -self.radius*size, 0.0, self.radius*size/5.0)
		glVertex3f(self.radius*size/2.0 -self.radius*size, 0.0, self.radius*size/5.0)
		glVertex3f(self.radius*size/2.0 -self.radius*size, 0.0, -self.radius*size/5.0)
		glEnd()
		
		glPopMatrix()

	# RETIRAR ESSAS 2 FUNCAO SE NAO UTILIZAR ATAQUE DA RAQUETE
	def attack(self):
		self.attacking = True
		self.rotXIncrement = 250.0
		self.positionIncrement = 1.0

	def animate(self, interval):
		if (self.attacking):
			self.rotationX += self.rotXIncrement*interval
			self.position.z += self.positionIncrement*interval
			if (self.rotationX >= 45.0):
				self.rotXIncrement *= -1
				self.positionIncrement *= -1
			elif (self.rotationX < 0.0):
				self.position.z -= self.positionIncrement*(self.rotationX / self.rotXIncrement)
				self.rotXIncrement = 0.0
				self.positionIncrement = 0.0
				self.attacking = False
				self.rotationX = 0.0
